/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shooter;

import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.input.FlyByCamera;
import com.jme3.input.InputManager;
import com.jme3.math.Vector3f;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.ViewPort;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.builder.TextBuilder;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
import de.lessvoid.nifty.controls.label.builder.LabelBuilder;
import de.lessvoid.nifty.controls.textfield.builder.TextFieldBuilder;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.TextRenderer;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ulfgur
 */
public class NiftyManager
{
    private SimpleApplication app;
    private ViewPort guiViewPort;
    private AppStateManager stateManager;
    private InputManager inputManager;
    private AssetManager assetManager;
    private FlyByCamera flyCam;
    private NiftyJmeDisplay niftyDisplay;
    private Nifty mainNifty;
    
    /**
     * this is a place where I can put all my
     * nifty screens and controllers,
     * from my start screen to the HUDs.
     */
    public NiftyManager(SimpleApplication a)
    {
        app = a;
        setUp(a);
        
      niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, null, guiViewPort);
      mainNifty = niftyDisplay.getNifty();
      guiViewPort.addProcessor(niftyDisplay);
      mainNifty.loadStyleFile("nifty-default-styles.xml");
      mainNifty.loadControlFile("nifty-default-controls.xml");
      
      
      setUpStartScreen();
      setUpHUDScreen();
      
      goToScreen("Main_Menu");
    }
    
    /**
     * allows me to directly reference game
     * assets.
     * @param a 
     */
    public void setUp(SimpleApplication a)
    {
      this.stateManager = this.app.getStateManager();
      this.inputManager = this.app.getInputManager();
      this.assetManager = this.app.getAssetManager();
      this.flyCam = this.app.getFlyByCamera();
      this.guiViewPort = this.app.getGuiViewPort();
    }
    
    /**
     * set up start screen
     */
    private void setUpStartScreen()
    {
        mainNifty.addScreen("Main_Menu", new ScreenBuilder("Main_Menu")
        {{
            controller(startScreen);

            // <layer>
            //main layer
            layer(new LayerBuilder("MainLayer")
            {{
                childLayoutVertical(); // layer properties, add more...

                // <panel>
                //this panel is for the Big Load Button
                panel(new PanelBuilder("Bigbutton_Panel")
                {{
                    childLayoutVertical();         
                    height("50%");
                    width("100%");
                    control(new ButtonBuilder("New Game", "New Game")
                    {{
                        alignCenter();
                        valignCenter();
                        height("90%");
                        width("90%");
                        visibleToMouse(true);
                        interactOnClick("newGame()");
                    }});
                }});
                // </panel>
                // <panel>
                //this panel is for the options buttons
                panel(new PanelBuilder("Misc_Panel")
                {{
                    childLayoutVertical();         
                    height("50%");
                    width("100%");
                    control(new ButtonBuilder("Load", "Load")
                    {{
                        alignCenter();
                        height("10%");
                        width("60%");
                        visibleToMouse(true);
                        //interactOnClick("configureLoadGame()");
                    }});

                    control(new ButtonBuilder("Options", "Options")
                    {{
                        alignCenter();
                        height("10%");
                        width("60%");
                        visibleToMouse(true);
                    }});
                    control(new ButtonBuilder("Exit", "Exit")
                    {{
                        alignCenter();
                        height("10%");
                        width("60%");
                        visibleToMouse(true);
                        interactOnClick("newGame()");
                    }});
                }});
                // </panel>
              }});
            // </layer>
          }}.build(mainNifty));
          // </screen>
    }
    
    /**
     * set up HUD
     */
    private void setUpHUDScreen()
    {
        mainNifty.addScreen("HUD", new ScreenBuilder("HUD")
        {{
            controller(hudScreen);

            // <layer>
            //main layer
            layer(new LayerBuilder("MainLayer")
            {{
                childLayoutVertical(); // layer properties, add more...
                this.visibleToMouse(false);
                // <panel>
                //this panel is for the Big Load Button
                panel(new PanelBuilder("Empty_Panel")
                {{
                    childLayoutVertical();         
                    height("90%");
                    width("100%");
                }});
                // </panel>
                // <panel>
                //this panel is for the options buttons
                panel(new PanelBuilder("HUDBar")
                {{
                    childLayoutHorizontal();      
                    height("10%");
                    width("100%");
                }});
                // </panel>
              }});
            // </layer>
          }}.build(mainNifty));
          // </screen>
    }
    
    /**
     * moves to a particular screen given the screen ID
     * @param s 
     */
    public void goToScreen(String s)
    {
        mainNifty.gotoScreen(s);
    }
    
    
    
    public StartScreenControl startScreen = new StartScreenControl();
    /**
     * Controls actions in the start screen.
     */
    public class StartScreenControl implements ScreenController
    {
        public void newGame()
        {
            goToScreen("HUD");
        }

        public void bind(Nifty nifty, Screen screen)
        {
        }

        public void onStartScreen() 
        {
            app.getFlyByCamera().setDragToRotate(true);
        }

        public void onEndScreen()
        {
            app.getFlyByCamera().setDragToRotate(false);
        }
    }
    
    
    public HUDScreenControl hudScreen = new HUDScreenControl();
    /**
     * Controls actions in hud screen
     */
    public class HUDScreenControl implements ScreenController
    {
        public void bind(Nifty nifty, Screen screen)
        {
        }

        public void onStartScreen() 
        {
            app.getFlyByCamera().setDragToRotate(false);
        }

        public void onEndScreen()
        {
            //app.getFlyByCamera().setDragToRotate(false);
        }
    }
    
    
}
